/*
 *  Date.c
 *
 *	Created on 9/10/2013
 *		Authors: Joseph DeLong, Justin Hamric, Alex Newcomer, Alex Bouvy
 */
 
 /*	Date function
 *		Command: date (-s yyyy mm dd)
 *			If the command, date, is submitted to the terminal, the date is displayed. 
 *			If the command is submitted with the -s flag and an 8 digit number, if the date is valid, the system date will be set to the provided date
 *					If the date submitted is not valid, a message will be sent to the terminal saying so.
 *					
 */
 
 //notes: linker errors
	//fixed by deleting the project and recreating
	
 
 typedef struct date_record {
	int day;
	int month;
	int year;
} date_record;
 
void date(char* input_tok[6], int command_args){
	int i, j;
	int swapper;
	int daysInFebruary;
	int foundACharacter = 0;
	date_record currentDate, newDate;
	
				if(command_args == 4){
				//error check
				if(abs((strncmp(input_tok[1], "-s", 2)))){
					// user entered invalid first arg
					printf("date -> First argument is an invalid switch.\n");
				} else if(!(strncmp(input_tok[1], "-s", 2))){
				//Alex's 9/19/2013 fixing the digit detection issue
				foundACharacter = 0;
				for(j=2; j < 5; j++){ 
					for(i=0; i < strlen(input_tok[j]); i++){
						if(isalpha(input_tok[j][i])){
							foundACharacter = 1;
						}
					}
				}
				if(foundACharacter){
					printf("date -> You must enter digits not characters.\n");
				} 
				//end Alex 9/19/2013
				else
				
					//check for yyyy(2) mm(3) dd(4), valid, not leapyear.
					if(atoi(input_tok[3]) > 12 || atoi(input_tok[3]) < 1){
						//month too big
						printf("date -> too many months\n");
					} else {
						//months and years are OK, check valid days
						//if february
						if(atoi(input_tok[3]) == 2){
							int leap = 0;
							if(atoi(input_tok[2])%400 == 0){
								leap = 1;
							} else if (atoi(input_tok[2])%100 == 0){
								leap = 0;
							} else if(atoi(input_tok[2])%4 == 0){
								leap = 1;
							} else {
								leap = 0;
							}
							if(leap){
								daysInFebruary = 29;
							} else {
								daysInFebruary = 28;
							}
							if(atoi(input_tok[4]) <= daysInFebruary){
								//call date
								date(input_tok, command_args);
							} else {
								//exit with error (leap year)
								printf("date -> leap year violation\n");
							}


						} else {//not february
							switch(atoi(input_tok[3])){
							case 1: case 3: case 5: case 7: case 8: case 10: case 12: 
								if((atoi(input_tok[4]) <= 31) && ( strlen(input_tok[2]) < 6) ){
									//set date
									newDate.month = atoi(input_tok[4]);
									newDate.year = atoi(input_tok[2]);
									newDate.day = atoi(input_tok[3]);
									sys_set_date(&newDate);
								} else {
									printf("date -> input error\n");
								}								
								break;
							case 4: case 6: case 9: case 11:
								if(( atoi(input_tok[4]) <= 30 ) && ( strlen(input_tok[2]) < 6)){
									//set date
									newDate.month = atoi(input_tok[4]);
									newDate.year = atoi(input_tok[2]);
									newDate.day = atoi(input_tok[3]);
									sys_set_date(&newDate);

								} else {
									printf("date -> your days are invalid\n");
								}
								break;
							}
						}
					}
				}
				//date(input_tok, command_args);
			} else if(command_args != 0){
				printf("date -> You have entered the wrong number of arguments\n");
			} else { //no args, display the date!
						sys_get_date(&currentDate);
						printf("%d, %d, %d\n", currentDate.day, currentDate.month, currentDate.year);
			}
}